// 德国o2
//! 已停止
const { createMobile, config } = require("../../../common/mobile");
const {
  info,
  saveFile,
  logging,
  timeout,
  reporter
} = require("../../../common/util");

const puppeteer = require("puppeteer");
const moment = require("moment");

const name = "o2";
const today = moment().format("YYYY-MM-DD");
const logdir = `./logs/${today}/de/o2/${info.deviceid}`;

const r = reporter(name);
const logger = logging(name, logdir);

(async () => {
  const browser = await puppeteer.launch(config);
  const page = await browser.newPage();

  const m = await createMobile(page);

  //sub status linstener
  m._page.on("response", response => {
    let url = response.url();
    let status = response.status();

    if (url.startsWith("http")) {
      logger.info(`${response.request().method()}`, status, url);
    }

    if (url.startsWith("https://services.dimoco.at/dcb/AT/mobilkom/redir")) {
      r.s("success");
    }

    if (url.startsWith("http://google.com")) {
      r.w("failure", url);
      m.close();
    }
  });

  try {
    r.i("start");

    logger.info("start");

    await m.get(`http://offer.lpaosub.com/offer/track?of=1016`);

    //等待页面加载完成
    await m._page.waitForSelector("button[class='btn btn-warning btn-lg']", {
      timeout
    });

    await m.sleep(500);

    //保存页面
    saveFile(`${logdir}/1.html`, await m._page.content());

    // 第一次点击
    await m._page.evaluate(() => {
      document.getElementsByClassName("btn btn-warning btn-lg")[0].click();
    });

    r.i("sub_click");
    logger.info("click1");

    await m._page.waitForNavigation({ timeout });

    await m._page.waitForSelector("button[id='kaufenBtn']", { timeout });

    //保存页面
    saveFile(`${logdir}/2.html`, await m._page.content());

    // 第二次点击
    await m.tapElement("button[id='kaufenBtn']");

    r.i("sub_click");
    logger.info("click2");

    await m._page.waitForNavigation({ timeout });

    await m._page.waitForSelector("input[id='tan']", { timeout });
    await m._page.waitForSelector("input[id='ok-button']", { timeout });

    //保存页面
    saveFile(`${logdir}/3.html`, await m._page.content());

    async function parseSms(text) {
      let code = null;

      // 英国
      let matchResultUK = text.match(
        /You OTP to subscribe content service is/g
      );
      // 泰语
      let matchResultTY = text.match(
        /You OTP to subscribe content service is/g
      );

      if (matchResultUK) {
        code = text.substr(40, 4);
      } else if (matchResultTY) {
        code = text.substr(40, 4);
      }

      if (code && code.length === 5 && Number(code)) {
        //填入文本
        await m._page.type("input[id='tan']", code);

        await m.sleep(1000 * 3);

        //确认订阅
        await m.tapElement("input[id='ok-button']");
      } else {
        console.log("error message", text);
        await m.sms(parseSms);
      }
    }

    await m.sms(parseSms);

    logger.info("end");
    r.i("end");
  } catch (e) {
    saveFile(`${logdir}/error.html`, await m._page.content());
    r.e(e + "");
  } finally {
    await m.close();
  }
  await browser.close();
})();
